package d2_com.itheima.io;

import java.io.*;

/**
 * IO流 字节流
 */
public class Test {
    public static void main(String[] args) throws Exception {
        /**
         * 文件字节输入流
         */
        FileInputStream is = new FileInputStream(new File("F:\\Java code\\javasepro\\javasepro\\oop-app6\\1.txt"));
        //简化写法
        FileInputStream is2 = new FileInputStream("F:\\Java code\\javasepro\\javasepro\\oop-app6\\1.txt");

        FileInputStream is3 = new FileInputStream("F:\\Java code\\javasepro\\javasepro\\oop-app6\\1.txt");
        //每次读取一个字节，如果没有数据了，返回-1
//        int read = is.read();
//        System.out.println((char)read);

        int b;
        while ((b = is.read()) != -1) {
            System.out.print((char) b);
        }
        is.close();

        System.out.println("\n--------------------");

        //字节数选择读取   ，存在数组里
//        byte[] buffer = new byte[3];
//        int len = is2.read(buffer);
//        String rs = new String(buffer);
//        System.out.println(rs);
//        System.out.println(len);

        byte[] buffer = new byte[3];
        int len;
        while ((len = is2.read(buffer)) != -1) {
            String rs = new String(buffer, 0, len);
            System.out.print(rs);
        }
        is2.close();

        System.out.println("\n--------------------");

        //一次性读取全部内容（文件不能过大）
        byte[] bytes = is3.readAllBytes();
        System.out.println(new String(bytes));
        is.close();

        /**
         * 文件字节输出流
         */

        OutputStream f1 = new FileOutputStream("oop-app6\\2.txt",true);
        byte[] bytes1 = "我是你爹".getBytes();
        f1.write(bytes1);
        f1.write("\r\n".getBytes());
        f1.write(bytes1,0,12);
        f1.close();

        /**
         * 复制照片
         */
        InputStream fo = new FileInputStream("oop-app6\\2.txt");
        OutputStream fi = new FileOutputStream("oop-app6\\3.txt");
        byte[] tp = fo.readAllBytes();
        fi.write(tp);
        fi.close();
        fo.close();
    }
}
